<?php
namespace app;

use Workerman\Connection\TcpConnection;

class Pusher
{
    // 存储所有客户端连接及相关信息
    protected static $clients = [];
    // 存储分组信息，键为分组名，值为连接 ID 数组
    protected static $groups = [];
    public function onConnect(TcpConnection $connection)
    {
        // 假设这里的 userId 是通过某种身份验证机制获取的，暂时设为一个示例值
        $userId = uniqid();
        self::$clients[$connection->id] = [
            'connection' => $connection,
            'userId' => $userId,
            'group' => null // 初始时不分组
        ];
        echo "onConnect\n";
    }

    public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
    {
        echo "onWebSocketConnect\n";
    }

    public function onMessage(TcpConnection $connection, $data)
    {
        $senderId = self::$clients[$connection->id]['userId'];
        $data = json_decode($data, true);

        if ($data && isset($data['type'])) {
            switch ($data['type']) {
                case 'single':
                    if (isset($data['targetId'])) {
                        $this->sendToSingleClient($data['targetId'], $data['message'], self::$clients);
                    }
                    break;
                case 'group':
                    if (isset($data['groupName'])) {
                        $this->sendToGroup($data['groupName'], $data['message'], self::$clients, self::$groups);
                    }
                    break;
                case 'broadcast':
                    $this->broadcast($data['message'], self::$clients);
                    break;
                case 'joinGroup':
                    if (isset($data['groupName'])) {
                        $groupName = $data['groupName'];
                        if (!isset(self::$groups[$groupName])) {
                            self::$groups[$groupName] = [];
                        }
                        if (!in_array($connection->id, self::$groups[$groupName])) {
                            self::$groups[$groupName][] = $connection->id;
                            self::$clients[$connection->id]['group'] = $groupName;
                        }
                        $connection->send("你已加入分组 $groupName");
                    }
                    break;
            }
        } else {
            // 普通消息广播
            $message = "用户 $senderId 说: $data";
            $this->broadcast($message, self::$clients);
        }
//        $connection->send($data);
    }

    public function onClose(TcpConnection $connection)
    {
        echo "onClose\n";
    }

    // 封装单条发送功能
    public function sendToSingleClient($clientId, $message, &$clients) {
        if (isset($clients[$clientId])) {
            $clients[$clientId]['connection']->send($message);
            return true;
        }
        return false;
    }

    // 封装分组发送功能
    public function sendToGroup($groupName, $message, &$clients, &$groups) {
        if (isset($groups[$groupName])) {
            foreach ($groups[$groupName] as $clientId) {
                if (isset($clients[$clientId])) {
                    $clients[$clientId]['connection']->send($message);
                }
            }
            return true;
        }
        return false;
    }

    // 封装广播功能
    public function broadcast($message, &$clients) {
        foreach ($clients as $client) {
            $client['connection']->send($message);
        }
    }
}